1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
import android.content.Context; import android.content.SharedPreferences;
import com.google.gson.Gson; import com.google.gson.reflect.TypeToken;
import java.util.List;
public class SPUtils {
private static final String LIST_TAG = ".LIST"; private static SharedPreferences sharedPreferences; private static Gson gson;
public static void init(Context context) { sharedPreferences = context.getSharedPreferences("shared_files", Context.MODE_PRIVATE); gson = new Gson(); }
private static void checkInit() { if (sharedPreferences == null || gson == null) { throw new IllegalStateException("Please call init(context) first."); } }
public static <T> void putData(T data) { putData(data.getClass().getName(), data); }
public static <T> void putData(String key, T data) { checkInit(); if (data == null) throw new IllegalStateException("data should not be null."); sharedPreferences.edit().putString(key, gson.toJson(data)).apply(); }
public static <T> void putData(List<T> data) { checkInit(); if (data == null || data.size() <= 0) throw new IllegalStateException( "List should not be null or at least contains one element."); Class returnType = data.get(0).getClass(); sharedPreferences.edit().putString(returnType.getName() + LIST_TAG, gson.toJson(data)).apply(); }
public static <T> T getData(Class<T> clz) { return getData(clz.getName(), clz); }
public static <T> T getData(String key, Class<T> clz) { checkInit(); String json = sharedPreferences.getString(key, ""); return gson.fromJson(json, clz); }
public static <T> List<T> getData(Class<List> clz, Class<T> gClz) { checkInit(); String json = sharedPreferences.getString(gClz.getName() + LIST_TAG, ""); return gson.fromJson(json, new TypeToken<List>(){}.getType()); }
public static void putString(String key, String data) { sharedPreferences.edit().putString(key, data).apply(); }
public static String getString(String key) { return sharedPreferences.getString(key, ""); }
public static void putInt(String key, int data) { sharedPreferences.edit().putInt(key,data).apply(); }
public static int getInt(String key) { return sharedPreferences.getInt(key, -1); }
public static void putBoolean(String key, boolean data) { sharedPreferences.edit().putBoolean(key,data).apply(); }
public static boolean getBoolean(String key,boolean defaultData) { return sharedPreferences.getBoolean(key, defaultData); }
public static void putFloat(String key, float data) { sharedPreferences.edit().putFloat(key,data).apply(); }
public static float getFloat(String key,float defaultData) { return sharedPreferences.getFloat(key, defaultData); }
public static void putLong(String key, long data) { sharedPreferences.edit().putLong(key,data).apply(); }
public static float getLong(String key,long defaultData) { return sharedPreferences.getLong(key, defaultData); }
public static void clear() { sharedPreferences.edit().clear().apply(); }
public static void remove(String key) { sharedPreferences.edit().remove(key).apply(); }
public static void remove(Class clz) { remove(clz.getName()); }
public static void removeList(Class clz) { sharedPreferences.edit().remove(clz.getName() + LIST_TAG).apply(); } }
|